import es
import random
import playerlib

import wcs

raceName = "Night Elf"
race     = wcs.races[raceName]

""" Load the strings into the variable text """
text     = race.loadStrings()

""" ############################### """
""" ### Begin the configuration ### """
""" ############################### """

config   = race.config()

config.text("")
config.text("********************")
config.text("Evasion")
config.text("    Players have a chance to ")
config.text("    dodge damage from an enemy attack.")
config.text("********************")
config.text("")
evasionMaxLevel = config.cvar("evasionMaxlevel", 5, "The maximum level of the skill")
evasionLevelInterval = config.cvar("evasionLevelInterval", 2, "The intervals between skill upgrades")
evasionPercentage = config.cvar("evasionPercentage", 5, "The percentage each level gives to dodge an enemy attack.")

config.text("")
config.text("********************")
config.text("Thorns")
config.text("    Players have a chance to reflect")
config.text("    30% additional damage back to")
config.text("    the attacker when hurt.")
config.text("********************")
config.text("")
thornsMaxLevel      = config.cvar("thornsMaxLevel", 4, "The maximum level of the skill")
thornsLevelInterval = config.cvar("thornsLevelInterval", 1, "The intervals between skill upgrades")
thornsPercentage    = config.cvar("thornsPercentage", 8, "The percentage each level gives to reflecting damage.")

config.text("")
config.text("********************")
config.text("True Shot")
config.text("    Players have a 30% chance to deal")
config.text("    additional damage to the victim.")
config.text("********************")
config.text("")
trueMaxLevel = config.cvar("trueMaxlevel", 4, "The maximum level of the skill")
trueLevelInterval = config.cvar("trueLevelInterval", 1, "The intervals between skill upgrades")
truePercentage = config.cvar("truePercentage", 15, "Each level gives this percentage of additional damage")

config.text("")
config.text("********************")
config.text("Entangle")
config.text("    This ultimate stops all moving")
config.text("    enemies in range. The higher the")
config.text("    skill level, the larger the range")
config.text("    and the longer theyre frozen for.")
config.text("********************")
config.text("")
entangleMinLevel = config.cvar("entangleMinLevel", 7, "The race level required for this skill")
entangleMaxLevel = config.cvar("entangleMaxLevel", 3, "The maximum level of the skill")
entangleLevelInterval = config.cvar("entangleLevelInterval", 2, "The intervals between skill upgrades")
entangleCoolDown = config.cvar("entangleCoolDown", 8, "How many seconds before a player can use the ultimate again")
entangleDuration = config.cvar("entangleDuration", 2, "Each level freezes victims for this amount of seconds")

config.write()
config.execute()

""" ##################### """
""" ### END OF CONFIG ### """
""" ##################### """

race.registerSkill("Evasion", int(evasionMaxLevel), int(evasionLevelInterval), "Have a chance to dodge damage from an attack.")
race.registerSkill("Thorns", int(thornsMaxLevel), int(trueLevelInterval), "Have a chance to reflect 30% damage back at your attacker.")
race.registerSkill("True Shot", int(trueMaxLevel), int(trueLevelInterval), "Have a 30% change to deal bonus damage.")
race.registerUltimate("Entangle", int(entangleMinLevel), int(entangleMaxLevel), int(entangleLevelInterval), "Use your ultimate to stop all nearby enemies from moving.")

def player_hurt(event_var):
    """
    Executed when a player is damaged. The two skills which are affected by
    this race is "True Shot" and "Thorns". 
    """
    userid   = event_var["userid"]
    attacker = event_var["attacker"]
    if attacker and int(attacker) != 0:
        wcsUserid = wcs.players[userid]
        wcsAttacker = wcs.players[attacker]
        damage = wcs.getDamage(event_var)
        thorns = wcsUserid[raceName]["Thorns"] * int(thornsPercentage)
        if thorns:
            if random.randint(1,100) <= thorns:
                wcsAttacker.damage(damage * .3, 32, userid)
                es.tell(userid, "#multi", text("thorns", {"attacker" : event_var["es_attackername"], "damage" : damage * 0.3}, playerlib.getPlayer(userid).get("lang")))
        trueshot = wcsUserid[raceName]["True Shot"] * (int(truePercentage) / 100.0)
        if trueshot:
            if random.randint(1,100) <= 30:
                wcsUserid.damage(damage * trueshot, 32, attacker)
                es.tell(attacker, "#multi", text("true shot", {"damage" : damage * trueshot}, playerlib.getPlayer(attacker).get("lang")))

def prep_hurt(event_var):
    """
    Executed before the player is hurt. Ensure that the victims health is
    increased before the engine removes the health from the player so it
    simulates that the attack was dodged.
    """
    userid   = int(event_var["userid"])
    attacker = int(event_var["attacker"])
    if attacker:
        damage  = wcs.getDamage(event_var)
        evasion = wcs.players[userid][raceName]["Evasion"]
        if evasion:
            if evasion >= random.randint(1,100):
                wcs.players[userid].saveLife(damage)
                es.tell(userid, "#multi", text("evasion", {"damage" : damage}, playerlib.getPlayer(userid).get("lang")))
                
def player_ultimate_on(event_var):
    """
    Executed when a players ultimate is pressed. See if they have an ultimate
    level, if so, entangle users by.
    """
    userid = int(event_var["userid"])
    wcsUserid = wcs.players[userid]
    entangle = wcsUserid[raceName]["Entangle"]
    if entangle:
        filter = "#ct" if event_var["es_userteam"] == "2" else "#t"
        wcsUserid.near(640, stun, filter)
        wcsUserid.setCoolDown("Entangle", float(entangleCoolDown))
                
def stun(userid, attacker):
    """
    Executed for every victim within the stunners range when he presseses his
    ultimate key. Ensure that the victim is stunned depending on the current
    level of the entangle level.
    
    @param int userid The victim who was in range of entangle
    @param int attacker The attacker; i.e the person casting the entangle
    """
    wcsUserid = wcs.players[userid]
    wcsAttacker = wcs.players[attacker]
    level = wcsAttacker[raceName]["Entangle"]
    timeLength = level * float(entangleDuration)
    wcsUserid.freeze(timeLength)
    es.tell(userid, "#lightgreen", text("entangle", lang = playerlib.getPlayer(userid).get("lang")))
